package com.cl.unipay4j.wx.pay;

import com.cl.unipay4j.core.*;
import com.cl.unipay4j.core.pay.IPayment;
import com.cl.unipay4j.core.pay.PayModel;
import com.cl.unipay4j.core.util.MoneyUtil;
import com.cl.unipay4j.wx.AbstractWxPay;
import com.cl.unipay4j.wx.sdk.WXPayConstants;
import lombok.extern.slf4j.Slf4j;

import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName AbstractAliPay
 * @Description 微信支付抽象类
 * @Author Small
 * @Date 2019/11/29 19:52
 * @Version 1.0
 **/
@Slf4j
public abstract class WxPay extends AbstractWxPay implements IPayment {
    public WxPay() {
    }

    public WxPay(IPayConfig payConfig) {
        super(payConfig);
    }

    /**
     * 方法描述: 封装支付请求参数
     * @Author Small
     * @param model
     * @Date 2019/11/29 10:01
     * @return java.util.HashMap<java.lang.String, java.lang.String>
     */
    protected HashMap<String, String> wapperPayRequestData(PayModel model, WxTradeTypeEnum tradeType) {
        HashMap<String, String> data = new HashMap<String, String>(8);
        // 商品描述
        data.put("body", model.getBody());
        // 商户订单号
        data.put("out_trade_no", model.getOutTradeNo());
        // 币种
        data.put("fee_type", "CNY");
        // 支付金额
        String amount = String.valueOf(MoneyUtil.yuan2Cent(model.getPrice()));
        data.put("total_fee", amount);
        // 乘客终端IP
        data.put("spbill_create_ip", model.getSpbillCreateIp());
        // 交易类型  付款码支付无需传递
        if (!WxTradeTypeEnum.MICROPAY.equals(tradeType)) {
            data.put("trade_type", tradeType.getValue());
        }
        // 付款码
        if (WxTradeTypeEnum.MICROPAY.equals(tradeType)) {
            data.put("auth_code", model.getAuthCode());
        }
        // 用户标识
        if (needOpenId()) {
            data.put("openid", model.getOpenid());
        }
        // 分账标识
        data.put("profit_sharing",model.getProfitSharing().getValue());
        data.put("notify_url",model.getNotifyUrl());
        return data;
    }


    protected Result doPay(PayModel model, WxTradeTypeEnum tradeType) {
        String outTransNo = model.getOutTradeNo();
        log.info("WxPay::{}=>订单{} 支付请求到达", tradeType, outTransNo);
        HashMap<String, String> data = wapperPayRequestData(model, tradeType);
        try {
            wxPayConfig.setTradeType(tradeType);
            Map<String, String> r = initWxPay().unifiedOrder(data);
            log.info("WxPay::{}=>订单{} 下单返回报文:{}", tradeType, outTransNo, r);
            String returnCode = r.get(WXPayConstants.RETURN_CODE);
            if (UnipayConst.SUCCESS.equals(returnCode)) {
                if (UnipayConst.SUCCESS.equals(r.get(WXPayConstants.RESULT_CODE))) {
                    log.info("WxPay::{}=>订单{} 下单成功", tradeType, outTransNo);
                    return doTradeSuccess(r);
                } else {
                    String errCode = r.get(WXPayConstants.ERR_CODE);
                    String errCodeDes = r.get(WXPayConstants.ERR_CODE_DES);
                    log.info("WxPay::{}=>订单{} 下单失败,错误代码:{},错误描述:{}", tradeType, outTransNo, errCode, errCodeDes);
                    return Result.returnFail(errCode, errCodeDes);
                }
            } else {
                String returnMsg = r.get(WXPayConstants.RETURN_MSG);
                log.info("WxPay::{}=>订单{} 下单失败,错误代码:{},错误描述:{}", tradeType, outTransNo, returnCode, returnMsg);
                return Result.returnFail(returnCode, returnMsg);
            }
        } catch (Exception e) {
            log.warn("WxPay::{}=>订单{} 下单时发生异常", tradeType, outTransNo, e);
        }
        return Result.exception();
    }

    /**
     * 是否需要openId参数
     * @return
     */
    public abstract boolean needOpenId();


    public abstract Result doTradeSuccess(Map<String, String> r) throws Exception;


    public static void main(String[] args) {
        log.info("WxPay::{}=>订单{} 支付请求到达", WxTradeTypeEnum.APP, "123456");
    }

}
